/**
 * @file Axios的Vue插件（添加全局请求/响应拦截器）
 */

import axios from 'axios'

const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 5000 // request timeout
})

service.install = (Vue) => {
    Vue.prototype.$axios = axios
}

service.getModel= function (url, params,showLoading=true) {
    let that = this
    params.showLoading = showLoading
    for(var key in params){
      if(!params[key]||params[key]=='null'){
        delete params[key]
      }
    }
    return axios({
        method: 'post',
        url: window.server_root + url,
        data: params,
        timeout: 10000
    })
}
service.syncRequest = function (url,data) {
    var xhr = new XMLHttpRequest();
    xhr.open('post', window.server_root + url ,false);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    if(data){
        xhr.send(data);
        
    }else{
        xhr.send();
    }
    return JSON.parse(xhr.responseText)
}

export default service
